package com.rem.flink.flink7Stream;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * 对流的操作
 * 合流 ->连接流ConnectedStreams  两条或以上的流都可以操作 类型可以不同
 * <p>
 * 连接流 将两个流合并到一个流中 类型可以不同但是不能超过2个
 *
 * @author Rem
 * @date 2022-10-17
 */

public class ConnectStreamTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Integer> stream1 = env.fromElements(1, 2, 3);
        DataStreamSource<Double> stream2 = env.fromElements(1.0, 2.0, 3.0);

        ConnectedStreams<Integer, Double> connectedStreams = stream1.connect(stream2);


        /**
         * < IN1 > -第一个输入的类型
         * < IN2 > -第二个输入的类型
         * < OUT > -输出类型
         */
        SingleOutputStreamOperator<String> result = connectedStreams.map(new CoMapFunction<Integer, Double, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return "Integer:" + value;
            }

            @Override
            public String map2(Double value) throws Exception {
                return "Double:" + value;
            }
        });

        result.print();


        env.execute();
    }
}
